<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Admin_UserInvite extends Controller_Admin_Base
{
    protected $need_auth = false; 
    
    public function action_index()
    {   
        $user_invite = $this->request->param('invite') ? $this->request->param('invite') : null;
        $user = ORM::factory('user')->get_user_by_invite($user_invite);
        
        if ($user->invite_status === Model_User::INVITE_STATUS_READY) {
            throw new Exception('По этому инвайту уже зарегистрировались');    
        } 
        
        if (HTTP_Request::POST == $this->request->method()) {
            $name = $this->request->post('name');
            $email = $this->request->post('invite_email');
            $password = $this->request->post('password');
                  
            try { 
                $user->edit_user_by_invite($email, $name, $password);
                
                Model_Auth::instance()->logout();
                Model_Auth::instance()->login($email, $password, true);                
                $_POST = array(); 
                
                if (Model_Auth::instance()->logged_in()) {
                    Request::current()->redirect('/dashboard'); 
                }      
            } catch (Model_Exception $e) {
                
            }
        } 
        
        $content = new View('admin/user/invite_tpl');
        $content->user = $user;
               
        $this->print_template('Регистрация по инвайту - '.$user->name, $content, null, true);    
    }
} 
